package dancrawler.db;

import com.almworks.sqlite4java.*;
import dancrawler.common.Misc;
import dancrawler.sound.Sound;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

/**
 * Database class
 *
 * @author Team4 / RMI HINGDA10
 */
public class Database {

  /**
   *
   */
  public String dbFileName;
  private SQLiteConnection db;
  private String path;
  private String fileTo;
  private String fileFrom;
  /**
   *
   */
  public String error; // Fjernes før siste compile

  /**
   *
   */
  public Database() {
    path = Misc.getAppdataPath("");
    fileTo = path + File.separatorChar + "dancrawler" + File.separatorChar + "dancrawler.s3db";
    error = null;

    try {
      fileFrom = URLDecoder.decode(this.getClass().getResource("/res/db/dancrawler.s3db").toString(), "UTF-8").replace("file:/", "");

      File dbSource = new File(fileFrom);
      File dbDestination = new File(fileTo);
      if (!dbDestination.exists()) {
        dbDestination.createNewFile();
        try (
                FileChannel in = new FileInputStream(dbSource).getChannel();
                FileChannel out = new FileOutputStream(dbDestination).getChannel()) {
          out.transferFrom(in, 0, in.size());
        } catch (Exception e) {
          error += "Filkopiering";
        }
      }
    } catch (Exception e) {
      error += "Ytre loop";
    }
    System.out.println(path);
    dbFileName = fileTo;
  }

  private void Connect() {
    db = new SQLiteConnection(new File(this.dbFileName));
    error += "Connect";
  }

  /**
   *
   * @return Character/monster skill mappings from SQL
   */
  public Map<Integer, Stats> getStatsFromSQL() {
    Map<Integer, Stats> statMap = new HashMap();

    try {
      Connect();
      db.open(true);
      SQLiteStatement st = db.prepare("SELECT * FROM creatures");
      int j = 0;
      try {
        while (st.step()) {
          statMap.put(st.columnInt(0),
                  new Stats(st.columnInt(0),
                  st.columnString(1),
                  st.columnInt(2),
                  st.columnInt(3),
                  st.columnInt(4),
                  st.columnInt(5),
                  st.columnInt(6),
                  st.columnInt(7),
                  st.columnString(8),
                  st.columnInt(9)));
          j++;
        }

      } catch (SQLiteException e) {
        error += "ledd 2";
      } finally {
        st.dispose();
      }

      db.dispose();
    } catch (SQLiteException e) {
      error += "ledd 1";
    }
    return statMap;
  }

  /**
   *
   * @return Sound mappings from SQL (event and filename)
   */
  public Map<String, Sound> getSoundsFromSQL() {
    Map<String, Sound> soundMap = new HashMap();

    try {
      Connect();
      db.open(true);
      SQLiteStatement st = db.prepare("SELECT * FROM sounds");
      int j = 0;
      try {
        while (st.step()) {
          soundMap.put(st.columnString(0), new Sound(st.columnString(0),
                  st.columnString(1)));
          j++;
        }

      } catch (SQLiteException e) {
        error += "ledd 2" + e;
      } finally {
        st.dispose();
      }

      db.dispose();
    } catch (SQLiteException e) {
      error += "ledd 1" + e;
    }
    return soundMap;
  }
}
